package com.example.home.controller;

import com.example.home.Dao.CollageRepository;
import com.example.home.Entities.Collage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("collages")
public class CollageController {

    @Autowired
    CollageRepository collageRepository;

    @GetMapping
    public String outStudentForm1(Model model){

        List<Collage> collage1 = collageRepository.findAll();
        model.addAttribute("collage2",collage1);
        return "collage/list-collage";
    }



    @GetMapping("/new")
    public String outStudentForm(Model model){

        Collage collage = new Collage();
        model.addAttribute("collage",collage);
        return "collage/Collage";
    }

    @PostMapping("/save")
    public String CreateStudent(Model model,Collage collage){

        //处理保存数据库的行为
        collageRepository.save(collage);
        //使用定位防止重复提交
        return "redirect:/collages/new";

    }

}
